package com.googlecode.maratische.google.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.googlecode.maratische.google.BaseException;
import com.googlecode.maratische.google.dao.PropertyDao;
import com.googlecode.maratische.google.model.Property;

public class PropertyDaoImpl extends BaseDaoImpl<Property> implements PropertyDao {

	public PropertyDaoImpl(Connection conn) {
		super(conn);
	}

	public Property createByRow(ResultSet rs) throws SQLException {
		Property property = new Property();
		property.setId(rs.getInt("ID"));
		property.setString1(rs.getString("STRING1"));
		property.setInteger1(rs.getInt("INTEGER1"));
		return property;
	}

	@Override
	protected String generateInsertSql() {
		return "insert into "+getTableName()+" (TYPE, STRING1,INTEGER1) values (?,?,?)";
	}

	protected String generateUpdateSql() {
		return "update "+getTableName()+" set TYPE = ?,STRING1 = ?,INTEGER1 = ? "+
		" where ID = ? ";
	}

	protected String getTableName() {
		return "PROPERTY";
	}

	protected PreparedStatement putInsertPrepareStatement(PreparedStatement pStat, Property status) throws SQLException {
		pStat.setString(1, status.getType());
		pStat.setString(2, status.getString1());
		pStat.setInt(3, status.getInteger1());
		return pStat;
	}
	
	@Override
	protected PreparedStatement putUpdatePrepareStatement(PreparedStatement pStat, Property status) throws SQLException {
		pStat = putInsertPrepareStatement(pStat, status);
		pStat.setInt(4, status.getId());
		return pStat;
	}

	public Property getPropertyByType(String type) throws BaseException {
		 List<Property> properties;
		try {
			properties = list("TYPE = ?", null, new Object[] {type});
			 if (properties != null && properties.size()>0) {
				 return properties.get(0);

			 }
			//создание значения по умолчанию
			 /**
			  * TODO сделать нормальную инициализацию
			  */
			if (Property.PROPERTY_FORM_WIDTH.equals(type)) {
				Property property = new Property();
				property.setType(Property.PROPERTY_FORM_WIDTH);
				property.setInteger1(1200);
				return property;
			}
			if (Property.PROPERTY_FORM_HEIGHT.equals(type)) {
				Property property = new Property();
				property.setType(Property.PROPERTY_FORM_HEIGHT);
				property.setInteger1(600);
				return property;
			}
			if (Property.PROPERTY_OFFLINE_STATUS.equals(type)) {
				Property property = new Property();
				property.setType(Property.PROPERTY_OFFLINE_STATUS);
				property.setInteger1(0);
				return property;
			}
			if (Property.PROPERTY_FEED_MENU_VIEW_ALL.equals(type)) {
				Property property = new Property();
				property.setType(Property.PROPERTY_FEED_MENU_VIEW_ALL);
				property.setInteger1(0);
				return property;
			}
			if (Property.PROPERTY_ITEM_MENU_VIEW_ALL.equals(type)) {
				Property property = new Property();
				property.setType(Property.PROPERTY_ITEM_MENU_VIEW_ALL);
				property.setInteger1(0);
				return property;
			}
			if (Property.PROPERTY_MAINPANE_DIVIDER.equals(type)) {
				Property property = new Property();
				property.setType(Property.PROPERTY_MAINPANE_DIVIDER);
				property.setInteger1(100);
				return property;
			}
			if (Property.PROPERTY_RIGHTPANE_DIVIDER.equals(type)) {
				Property property = new Property();
				property.setType(Property.PROPERTY_RIGHTPANE_DIVIDER);
				property.setInteger1(100);
				return property;
			}
			if (Property.PROPERTY_DISPLAY_PANE_FONT_SIZE.equals(type)) {
				Property property = new Property();
				property.setType(Property.PROPERTY_DISPLAY_PANE_FONT_SIZE);
				property.setInteger1(15);
				return property;
			}
			if (Property.PROPERTY_LOGIN.equals(type)) {
				Property property = new Property();
				property.setType(Property.PROPERTY_LOGIN);
				property.setString1("");
				property.setInteger1(0);
				return property;
			}
			if (Property.PROPERTY_AUTH.equals(type)) {
				Property property = new Property();
				property.setType(Property.PROPERTY_AUTH);
				property.setString1("");
				property.setInteger1(0);
				return property;
			}
			if (Property.PROPERTY_SID.equals(type)) {
				Property property = new Property();
				property.setType(Property.PROPERTY_SID);
				property.setString1("");
				property.setInteger1(0);
				return property;
			}
			if (Property.PROPERTY_L_SID.equals(type)) {
				Property property = new Property();
				property.setType(Property.PROPERTY_L_SID);
				property.setString1("");
				property.setInteger1(0);
				return property;
			}
			if (Property.PROPERTY_LOCALE_LANGUAGE.equals(type)) {
				return createProperty(Property.PROPERTY_LOCALE_LANGUAGE,"",0);
			}
			if (Property.PROPERTY_LOCALE_COUNTRY.equals(type)) {
				return createProperty(Property.PROPERTY_LOCALE_COUNTRY,"",0);
			}
			if (Property.PROPERTY_PROXY_ENABLED.equals(type)) {
				return createProperty(Property.PROPERTY_PROXY_ENABLED,"",0);
			}
			if (Property.PROPERTY_PROXY_HOST.equals(type)) {
				return createProperty(Property.PROPERTY_PROXY_HOST,"",0);
			}
			if (Property.PROPERTY_PROXY_PORT.equals(type)) {
				return createProperty(Property.PROPERTY_PROXY_PORT,"",0);
			}
			if (Property.PROPERTY_PROXY_LOGIN.equals(type)) {
				return createProperty(Property.PROPERTY_PROXY_LOGIN,"",0);
			}
			if (Property.PROPERTY_PROXY_PASSWORD.equals(type)) {
				return createProperty(Property.PROPERTY_PROXY_PASSWORD,"",0);
			}
		} catch (SQLException e) {
			throw new BaseException(e.getMessage());
		}
		return null;
	}

	public Property createProperty(String type, String string1, Integer integer1) {
		Property property = new Property();
		property.setType(type);
		property.setString1(string1);
		property.setInteger1(integer1);
		return property;
	}

}
